package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.component.BossBarComponent;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.damgesource.SkillDamageSource;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.sever.SeverCustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.CustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.sever.SeverCustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.reality.TestClientCustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.UUID;

import static net.xxcxcxcx.xc_enhancedmob.packet.damage_num.DamageNumPacket.sendDamageNum;

public class ArmorShieldSkill extends BaseSkill {
    public float ArmorCount;
    public float MaxArmorCount;
    public boolean ArmorBreak;
    public Text ArmorShieldValue = new LiteralText("护甲");
    public UUID ArmorShieldUUID = UUID.fromString("1f1132b0-1a77-aa11-e14d-ad65ef277ac4");
    public CustomBossBar bossBar = null;

    public ArmorShieldSkill(int level) {
        super(level);
    }

    public ArmorShieldSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public ArmorShieldSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "护甲技能，获得一个（10%+5%*level）*最大生命的护甲值，在护甲值存在时候会削减（20%+5%*level）最多95%的伤害并且分摊（60%+5%*level）最多95%的伤害，护甲破碎后会以固定值恢复。";
    }

    @Override
    public SkillRarity getSkillRarity() {
        return SkillRarity.Mystery;
    }

    @Override
    public NbtCompound getNbtCompound() {
        NbtCompound nbtCompound = super.getNbtCompound();
        nbtCompound.putFloat("ArmorCount", ArmorCount);
        nbtCompound.putFloat("MaxArmorCount", MaxArmorCount);
        nbtCompound.putBoolean("ArmorBreak", ArmorBreak);
        return nbtCompound;
    }

    @Override
    public void readbyNbtCompound(NbtCompound nbtCompound) {
        super.readbyNbtCompound(nbtCompound);
        ArmorCount = nbtCompound.getFloat("ArmorCount");
        MaxArmorCount = nbtCompound.getFloat("MaxArmorCount");
        ArmorBreak = nbtCompound.getBoolean("ArmorBreak");
    }

    @Override
    public int getEffectiveSequence() {
        return 100;
    }

    @Override
    public boolean onAttribute(MobEntity mobEntity) {
        MaxArmorCount = mobEntity.getMaxHealth() * (0.1f + 0.05f * getSkillLevel());
        ArmorCount = MaxArmorCount;
        ArmorBreak = false;
        if (((MobData) mobEntity).getComponent(BossBarComponent.class.getName()) instanceof BossBarComponent component) {
            if (component.bossBarComplete == null) {
                component.CreatbossBar();
            }
            if (component.bossBarComplete.getBossColumn(component.MainPart) instanceof SeverCustomBossBarColumn bossBarColumn) {
                bossBarColumn.addBossBar(new SeverCustomBossBar(
                        ArmorShieldUUID,
                        ArmorShieldValue,
                        new TestClientCustomBossBar(),
                        MaxArmorCount,
                        ArmorCount,
                        1, false, null, -2
                ));
            }
        }
        return true;
    }

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if (source instanceof SkillDamageSource source1 && source1.getPassSkill()) return true;
        setSkilltime(target.world.getTime() + 100);
        if (!ArmorBreak) {
            float injury = amount.getValue() * Math.max(0.05f, 0.8f - 0.05f * getSkillLevel());
            float Absorption = injury * (Math.min(0.95f, 0.6f + 0.05f * getSkillLevel()));
            sendDamageNum(target, injury, source.name);
            ArmorCount -= injury;
            if (ArmorCount <= 0) {
                ArmorCount = 0;
                ArmorBreak = true;
            }
            amount.setValue(injury - Absorption);
        }
        return true;
    }

    @Override
    public boolean passiveApply(MobEntity mobEntity) {
        if (ArmorBreak && mobEntity.world.getTime() % 5 == 0) {
            float Updata = 1.0f + getSkillLevel() / 4.0f;
            ArmorCount += Updata;
            if (ArmorCount >= MaxArmorCount) {
                ArmorCount = MaxArmorCount;
                ArmorBreak = false;
            }
        }
        if (mobEntity.isDead()) {
            ArmorCount = 0;
        }
        if (bossBar == null) {
            if (((MobData) mobEntity).getComponent(BossBarComponent.class.getName()) instanceof BossBarComponent component) {
                if (component.bossBarComplete != null) {
                    bossBar = component.bossBarComplete.getBossBar(ArmorShieldUUID);
                }
            }
        }
        if (bossBar != null) {
            bossBar.setNowCount(ArmorCount);
            bossBar.setMaxCount(MaxArmorCount);
        }
        return true;
    }
}
